package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.entity.FacilityInfo;
import com.atguigu.lease.model.entity.GraphInfo;
import com.atguigu.lease.model.entity.LabelInfo;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.LeaseAgreementService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    GraphInfoMapper graphInfoMapper;

    @Autowired
    LabelInfoMapper labelInfoMapper;

    @Autowired
    FacilityInfoMapper facilityInfoMapper;

    @Autowired
    RoomInfoMapper roomInfoMapper;


    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        ApartmentInfo apartmentInfo = this.getById(id);

        //图片
        LambdaQueryWrapper<GraphInfo> graphVoLambdaQueryWrapper =new LambdaQueryWrapper<>();
        graphVoLambdaQueryWrapper.eq(GraphInfo::getItemId, id)
                .eq(GraphInfo::getItemType, ItemType.APARTMENT);

        List<GraphInfo> graphInfos = graphInfoMapper.selectList(graphVoLambdaQueryWrapper);
        List<GraphVo> graphVoList =new ArrayList<>();
        for (GraphInfo graphInfo : graphInfos) {
            GraphVo graphVo =new GraphVo();
            BeanUtils.copyProperties(graphInfo, graphVo);
            graphVoList.add(graphVo);
        }
        //标签
         List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);

        //配套
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByApartmentId(id);


        //租金最小值
        BigDecimal minRent =  roomInfoMapper.selectMinRent(id);

        //公寓是否被删除

        ApartmentDetailVo apartmentDetailVo =new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setMinRent(minRent);
        apartmentDetailVo.setIsDelete(apartmentInfo.getIsRelease().getCode()==0);
        return apartmentDetailVo;
    }
}




